package cn.xawl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.xawl.gulimall.product.entity.AttrAttrgroupRelationEntity;
import cn.xawl.gulimall.product.entity.AttrEntity;
import cn.xawl.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.xawl.gulimall.product.entity.AttrGroupEntity;
import cn.xawl.gulimall.product.service.AttrGroupService;
import cn.xawl.common.utils.PageUtils;
import cn.xawl.common.utils.R;



/**
 * 属性分组
 *
 * @author lh
 * @email lhm49869999@163.com
 * @date 2020-06-17 14:15:22
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catalogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catalogId") Long catalogId){
        PageUtils page = attrGroupService.queryPage(params, catalogId);

        return R.ok().put("page", page);
    }

    /**
     * 获取分类下所有分组&关联属性
     * /product/attrgroup/{catalogId}/withattr
     */
    @GetMapping("/{catalogId}/withattr")
    public R attrGroupWithAttrs(@PathVariable Long catalogId) {
        List<AttrGroupWithAttrsVo> data = attrGroupService.getAttrGroupWithAttrsByCatalogId(catalogId);
        return R.ok().put("data", data);
    }

    /**
     * 获取属性分组没有关联的其他属性
     * /product/attrgroup/{attrgroupId}/noattr/relation
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R otherRelation(@RequestParam Map<String, Object> params, @PathVariable Long attrgroupId) {
        PageUtils page = attrGroupService.getAttrsByOtherRelation(params, attrgroupId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		//根据分类 id 查询该分类的完整路径
		List<Long> catalogPath =  attrGroupService.getCatalogPathById(attrGroup.getCatalogId());

		attrGroup.setCatalogPath(catalogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 获取指定分组关联的所有属性
     * /product/attrgroup/{attrgroupId}/attr/relation
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getRelationAttrs(@PathVariable Long attrgroupId) {
        List<AttrEntity> data = attrGroupService.getAttrByGroup(attrgroupId);
        return R.ok().put("data", data);
    }

    /**
     * 删除属性与分组的关联关系（可以删除多个）
     * /product/attrgroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntities) {
        attrGroupService.deleteRelations(relationEntities);
        return R.ok();
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 添加属性与分组关联关系
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> list){
        attrGroupService.addBatchRelation(list);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
